package castlewarserver;

import gameObjects.Castle;
import gameObjects.Unit;
import gui.GuiObject;
import gui.MainWindow;

import java.rmi.RemoteException;

import javax.swing.JOptionPane;

import upgrades.Upgrade;

public class Connector {

	private GameProxy gameProxy;
	private MainWindow mainWindow;
	public boolean connectionError = false;
	
	public Connector (String endpoint,MainWindow mainWindow){
		gameProxy = new GameProxy(endpoint);
		this.mainWindow=mainWindow;
	}

	public Connector (String endpoint){
		gameProxy = new GameProxy(endpoint);
	}

	public Castle getCastle(String player){
		Castle castle = null;
		connectionError = false;
		try {
			if (player.equalsIgnoreCase("ORANGE")){
				castle= gameProxy.getCastleOrange();
			}
			else{
				castle = gameProxy.getCastleBlue();
			}

		} catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return castle;
	}

	public Unit[] getAllUnits(){
		Unit[] units= null; 
		connectionError = false;
		try {
			units=gameProxy.getAllUnits();
		} catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return units;
	}
	public void loadCastles(){
		mainWindow.castleOrange = new GuiObject(getCastle("ORANGE"));
		mainWindow.castleBlue = new GuiObject(getCastle("BLUE"));
	}

	public void loadAllUnits(){
		Unit[] units=getAllUnits();
		if (units != null){
			for (int i = 0; i< units.length; i++){
				mainWindow.units.add(new GuiObject(units[i]));
			}
		}
	}
	public Unit moveUnit(Unit unit,int x, int y){
		connectionError = false;

		try {
			unit = gameProxy.moveUnit(unit,x,y);
		} catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return unit;
	}

	public Unit[] attackUnit( Unit attacker, Unit defender ){
		Unit[] pair= new Unit[] {attacker,defender};
		connectionError = false;
		try {
			pair = gameProxy.attackUnit(attacker, defender);
			attacker=pair[0];
			defender = pair[1];
		} catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return pair;
	}
	public Unit attackCastle(Unit unit){
		connectionError = false;
		try{
			unit = gameProxy.attackCastle(unit);
		}
		catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return unit;
	}

	public Unit buyUnit (String unitType,String player){
		Unit unit =null;
		connectionError = false;
		try{
			unit = gameProxy.buyUnit(unitType, player);
		}
		catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return unit;
	}

	public String newGame(String player){
		connectionError = false;
		try{
			player = gameProxy.newGame(player);
		}
		catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return player;
	}

	public void resetGame(){
		connectionError = false;
		try{
			gameProxy.resetGame();
		}
		catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
	}

	public boolean isPalyerTaken(String player){
		boolean result = false;
		connectionError = false;
		try{
			result = gameProxy.isPlayerTaken(player);
		}
		catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return result;
	}
	public String getActualPlayer (){
		String player = "";
		connectionError = false;
		try{
			player = gameProxy.getActualPlayer();
		}
		catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return player;
	}
	public void endTurn (String player){
		connectionError = false;
		try{
			gameProxy.endTurn(player);
		}
		catch (RemoteException e) {
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
	}
	public Upgrade[] getUpgrades(String player){
		Upgrade [] upgrades = null; 
		connectionError = false;
		try{
			upgrades=gameProxy.getUpgrades(player);
		}
		catch( RemoteException e){
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return upgrades;
	}
	public boolean buyUpgrade(String player, int index){
		boolean result = false;
		connectionError = false;
		try{
			result = gameProxy.buyUpgrade(player, index);
		}
		catch( RemoteException e){
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return result;
	}
	public boolean isEndOfGame(){
		boolean result = false;
		connectionError = false;
		try{
			result = gameProxy.isEndOfGame();
		}
		catch( RemoteException e){
			connectionError = true;
			exeptionHandler(e);
			e.printStackTrace();
		}
		return result;
	}
	
	private void exeptionHandler(Exception e){
		StringBuffer buff = new StringBuffer();
		buff.append("Connection problems. Error message:\n");
		buff.append(e.getMessage());
		JOptionPane.showMessageDialog(null, buff.toString());
	}
}
